﻿/*	
	stencil - action script 3 isometric game engine.

	http://code.google.com/p/stencilas3/
	
	Copyright (c) 2010 Max Zhuravkov, All Rights Reserved.

	Permission is hereby granted, free of charge, to any person obtaining a copy of
	this software and associated documentation files (the "Software"), to deal in
	the Software without restriction, including without limitation the rights to
	use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
	of the Software, and to permit persons to whom the Software is furnished to do
	so, subject to the following conditions:

	The above copyright notice and this permission notice shall be included in all
	copies or substantial portions of the Software.

	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
	SOFTWARE.
*/
package stencil.core.engine.scene {
	
	import flash.geom.Rectangle;
	import stencil.core.engine.Box;
	import stencil.core.engine.ISceneObject;
	import stencil.core.engine.Position;
	import stencil.core.engine.IGameObject;
	import stencil.core.engine.Bounds;
	import stencil.core.engine.Box;
	
	/**
	 * ...
	 * @author Max Zhuravkov
	 */
	public class SimpleSceneNode implements ISceneNode {
		
		private var _id:String;
		
		private var _position:Position;
		
		private var _rect:Rectangle;
		
		private var _bounds:Bounds;
		
		private var _dimensions:Box;
		
		private var _tiles:Array;
		
		private var _objects:Array;
		
		
		//----------------------------------------------------------------
		// Public methods
		//----------------------------------------------------------------
		
		public function SimpleSceneNode() {
			
		}
		
		//----------------------------------------------------------------
		// Public properties
		//----------------------------------------------------------------
		
		/**
		 * @inheritDoc
		 */
		public function set id(value:String):void {
			if (this._id == value) {
				return;
			}
			this._id = value;
		}
		
		public function get id():String {
			return this._id;
		}
		
		/**
		 * @inheritDoc
		 */
		public function set rect(value:Rectangle):void {
			if (this._rect && this._rect.equals(value)) {
				return;
			}
			if (value) {
				if (!this._rect) {
					this._rect = new Rectangle();
				}
				this._rect.x = value.x;
				this._rect.y = value.y;
				this._rect.width = value.width;
				this._rect.height = value.height;
			}else {
				this._rect = null;
			}
		}
		
		public function get rect():Rectangle {
			return this._rect;
		}
		
		/**
		 * @inheritDoc
		 */
		public function set bounds(value:Bounds):void {
			this._bounds = value;
		}
		
		public function get bounds():Bounds {
			return this._bounds;
		}
		
		/**
		 * @inheritDoc
		 */
		public function set tiles(value:Array):void {
			this._tiles = value;
		}
		
		public function get tiles():Array {
			return this._tiles;
		}
		
		/**
		 * @inheritDoc
		 */
		public function set objects(value:Array):void {
			this._objects = value;
		}
		
		public function get objects():Array {
			return this._objects;
		}
		
		/**
		 * @inheritDoc
		 */
		public function set position(value:Position):void {
			if (this._position && this._position.equals(value)) {
				return;
			}
			if (value) {
				if (!this._position) {
					this._position = new Position();
				}
				this._position.x = value.x;
				this._position.y = value.y;
				this._position.z = value.z;
			}else {
				this._position = null;
			}
		}
		
		public function get position():Position {
			return this._position;
		}
		
		/**
		 * @inheritDoc
		 */
		public function set dimensions(value:Box):void {
			this._dimensions = value;
		}
		
		public function get dimensions():Box {
			return this._dimensions;
		}
		
		//----------------------------------------------------------------
		// Public methods
		//----------------------------------------------------------------
		
		/**
		 * @inheritDoc
		 */
		public function put(o:ISceneObject):void {
			if (this.contains(o)) {
				return;
			}
			if (this.bounds.rect.intersects(o.bounds.rect)) {
				this._objects.push(o);
			}
		}
		
		/**
		 * @inheritDoc
		 */
		public function remove(o:ISceneObject):void {
			if (!this.contains(o)) {
				return;
			}
			var index:int = this._objects.indexOf(o);
			if (index >= 0) {
				this._objects.splice(index, 1);
			}
		}
		
		/**
		 * @inheritDoc
		 */
		public function contains(o:ISceneObject):Boolean {
			return this._objects.indexOf(o) >= 0;
		}
		
		public function toString():String {
			return "[SceneNode#" + this.id + "]"// + " " + this.bounds.rect + "]"
		}
		
		/**
		 * @inheritDoc
		 */
		public function get flagDisplayable():Boolean {
			return false;
		}
		
		/**
		 * @inheritDoc
		 */
		public function get flagStatic():Boolean {
			return true;
		}
		
		/**
		 * @inheritDoc
		 */
		public function dispose():void {
			while (this._objects.length) {
				this._objects.splice();
			}
		}
		
		public function get flagTile():Boolean {
			return true;
		}
		
		public function get flagBackground():Boolean {
			return false;
		}
		
		/**
		 * @inheritDoc
		 */
		public function get type():String {
			return null;
		}
	}

}